package org.devf.fork.imsimulator.service.api;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import org.devf.fork.imsimulator.http.ApiClientFactory;
import org.devf.fork.imsimulator.http.pojo.ApiResult;
import org.devf.fork.imsimulator.model.HttpConstant;
import org.devf.fork.imsimulator.service.TokenMgr;
import org.devf.fork.imsimulator.service.vo.UserExtendModel;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * UserService related
 *
 * @author wanghuafu
 * @since 1.0  2023/9/25 11:18
 */
public class UserApiClient {

    /**
     * 查用户信息
     * @param userId
     * @return
     */
    public static UserExtendModel getUserInfo(long userId) {
        String data = ApiClientFactory.getDefaultClient().get(HttpConstant.WebUrl.GET_USER, Map.of("userId", userId));
        ApiResult<UserExtendModel> fromUserInfoResult = JSONUtil.toBean(data, new TypeReference<>() {
        }, true);
        return fromUserInfoResult.getData();
    }


    /**
     * 查询好友列表
     * @return
     */
    public static List<UserExtendModel> getFriends(){
        Assert.notNull(TokenMgr.userModel,"未登录");
        String result = ApiClientFactory.getDefaultClient().execute("/friend/list",
                HttpConstant.ReqMethod.GET,
                null
        );
        ApiResult<List<UserExtendModel>> r = JSONUtil.toBean(result,new TypeReference<>(){},true);
        List<UserExtendModel> list =  r.getData();
        return list.stream()
                .filter(u-> !Objects.equals(u.getId(), TokenMgr.userModel.getId()))
                .collect(Collectors.toList());
    }


}
